package someTestExcemple.thread;

import java.util.concurrent.CompletableFuture;

public class CompletableFutureTest {
    public static void main(String[] args) {
        // 使用 ForkJoinPool 线程池
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("异步任务执行完成");
        });

        // 主线程继续执行其他任务
        System.out.println("主线程继续执行-1");

        // 等待异步任务完成
        future.join();
        System.out.println("+=====+");

        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "异步任务的结果";
        });

        // 主线程继续执行其他任务
        System.out.println("主线程继续执行-2");

        // 获取异步任务的结果
        String result = future1.join();
        System.out.println("异步任务的结果是: " + result);


    }
}
